package com.arvato.wxwork.service.store;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.arvato.minipro.bean.bean_dto.common.PageData;
import com.arvato.minipro.bean.bean_dto.common.Query;
import com.arvato.wxwork.bean.bean_do.store.WxworkAreaDo;
import com.arvato.wxwork.bean.bean_vo.store.WxworkAreaVo;
import com.arvato.wxwork.mapper.store.WxworkAreaMapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;

/**
 * 销售地区管理service
 * @author zhan655
 *
 */
@Service
@Transactional
public class WxworkAreaService {
	
	@Autowired
	private WxworkAreaMapper wxworkAreaMapper;
	
	/**
	 * 查询区域列表 分页查询
	 * @param query
	 * @param integer 
	 * @return
	 */
	public PageData listAreaByQuery(Query query, Integer pageNum, Integer pageSize) {
		// 设置分页参数
		PageHelper.startPage(pageNum, pageSize);
		List<WxworkAreaVo> areaList = wxworkAreaMapper.listAreaByQuery(query);
		PageInfo<WxworkAreaVo> pageInfo = new PageInfo<WxworkAreaVo>(areaList);
		PageData pageData = new PageData(pageInfo.getTotal(), pageInfo.getList());
		return pageData;
	}
	
	/**
	 * 根据ID查询详情
	 * @param areaId
	 * @return
	 */
	public WxworkAreaVo getAreaById(String areaId) {
		return wxworkAreaMapper.getAreaById(areaId);
	}

	/**
	 * 更新区域信息
	 * @param wxworkAreaDo
	 * @return
	 */
	public int insert(WxworkAreaDo wxworkAreaDo) {
		return wxworkAreaMapper.insert(wxworkAreaDo);
	}
	
	/**
	 * 更新区域信息
	 * @param wxworkAreaDo
	 * @return
	 */
	public int update(WxworkAreaDo wxworkAreaDo) {
		return wxworkAreaMapper.update(wxworkAreaDo);
	}

	/**
	 * 删除区域信息
	 * @param areaId
	 * @return
	 */
	public int delete(String areaId) {
		return wxworkAreaMapper.delete(areaId);
	}
	
	/**
	 * 检查所给省是否已经存在
	 * @param provinceList 
	 * @param areaId 
	 * @return
	 */
	public String check(String provinceList, String areaId) {
		if(provinceList == null || provinceList.equals("")) {
			return null;
		}
		String[] split = provinceList.split(",");
		Query query = new Query();
		for(String province : split) {
			query.put("province", province);
			List<WxworkAreaVo> areaList = wxworkAreaMapper.listAreaByQuery(query);
			if(areaList.size() >= 1 && !areaList.get(0).getAreaId().equals(areaId)) {
				return areaList.get(0).getAreaName() + "(" + areaList.get(0).getAreaCode() + ")下已经存在" + province;
			}
		}
		return null;
	}
	
	
}
